package cn.iocoder.yudao.module.wine.service.memberopenlog;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.wine.controller.admin.memberopenlog.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.memberopenlog.MemberOpenLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.memberopenlog.MemberOpenLogMapper;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 会员开通记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class MemberOpenLogServiceImpl implements MemberOpenLogService {

    @Resource
    private MemberOpenLogMapper memberOpenLogMapper;

    @Override
    public Long createMemberOpenLog(MemberOpenLogSaveReqVO createReqVO) {
        // 插入
        MemberOpenLogDO memberOpenLog = BeanUtils.toBean(createReqVO, MemberOpenLogDO.class);
        memberOpenLogMapper.insert(memberOpenLog);
        // 返回
        return memberOpenLog.getId();
    }

    @Override
    public void updateMemberOpenLog(MemberOpenLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberOpenLogExists(updateReqVO.getId());
        // 更新
        MemberOpenLogDO updateObj = BeanUtils.toBean(updateReqVO, MemberOpenLogDO.class);
        memberOpenLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberOpenLog(Long id) {
        // 校验存在
        validateMemberOpenLogExists(id);
        // 删除
        memberOpenLogMapper.deleteById(id);
    }

    private void validateMemberOpenLogExists(Long id) {
        if (memberOpenLogMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public MemberOpenLogDO getMemberOpenLog(Long id) {
        return memberOpenLogMapper.selectById(id);
    }

    @Override
    public PageResult<MemberOpenLogRespVO> getMemberOpenLogPage(MemberOpenLogPageReqVO pageReqVO) {
        return memberOpenLogMapper.selectPage(pageReqVO);
    }

    @Override
    public MemberOpenLogDO appGetMemberOpenLog(Long memberTypeId) {

        return memberOpenLogMapper.selectOne(new LambdaQueryWrapperX<MemberOpenLogDO>()
                .eq(MemberOpenLogDO::getMemberTypeId, memberTypeId)
                .eq(MemberOpenLogDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                        .orderByDesc(MemberOpenLogDO::getCreateTime)
                .last("limit 1"));
    }

    @Override
    public MemberOpenLogDO appGetMemberOpenLogInfo(String tradeNo) {

        return memberOpenLogMapper.selectOne(new LambdaQueryWrapperX<MemberOpenLogDO>()
                .eq(MemberOpenLogDO::getTradeNo, tradeNo)
                .orderByDesc(MemberOpenLogDO::getCreateTime)
                .last("limit 1"));
    }
    @Override
    public List<MemberOpenLogDO> getMemberOpenLogList(Collection<String> tradeNos) {

        return memberOpenLogMapper.selectList(new LambdaQueryWrapperX<MemberOpenLogDO>()
                .inIfPresent(MemberOpenLogDO::getTradeNo, tradeNos)
                .orderByDesc(MemberOpenLogDO::getCreateTime));
    }
}
